home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!tglbbs!charles.herold
- From: charles.herold@tglbbs.com (Charles Herold)
- Newsgroups: comp.lang.c
- Subject: array of struct pointer switching difficulties
- Date: Fri, 26 Jan 1996 18:48:17 GMT
- Message-ID: <96012819461829517@tglbbs.com>
- Organization: TGL BBS 3 Nodes of Fun 212-974-3925, 212-765-2524, 212-541-5596
- Distribution: world
-
- I posted a question regarding reversing an array of pointers. Two
- people posted similar answers, but neither works, so perhaps there is a
- more fundamental problem with my code. Therefore I have put together a
- full little program that will get the files of a directory, reverse the
- order they're in, and print them out, so people can see what I've done,
- and hopefully tell me what I'm doing wrong. In SortFiles, a version of
- which I posted last time, I have both the reverse sort I use and the one
- suggested to me. I apologize for posting such a long piece of code, but
- this seems to be the minimum necessary to show you what the problem is.
-
- Thanks to anyone willing to go through all this. This will compile in
- QuickC, and I believe if you were to replace _dos_findfirst/next with
- the appropriate functions that it would compile in anything else.
- /***************** CUT HERE *******************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <dos.h>
- #include <string.h>
-
- struct dir_window {
- int numberfiles; /* number files found */
- struct find_t **files;
- };
- struct find_t *files;
- int reverse_sort = 1; /* whether to reverse the order */
- struct dir_window window;
- /* next two declarations can be ignored, although the first will get
- * a link error warning. They are just here so I don't have
- * to rewrite a couple of things
- */
- int (*sorts[])();
- int sort_proc;
- /*resizeArray() resizes an array of struct find_t. Used with
- Directory()*/
- struct find_t *resizeArray( struct find_t *files, int size)
- {
- if( (files = realloc( files, sizeof( struct find_t) * size)) ==
- NULL) {
- perror( "" );
- fprintf( stderr, "Unable to allocate memory. size = %d\n",
- size);
- exit(1);
- }
- return files;
- }
- /*********************************************************
- * Directory() puts all specified files in find_t structure in files
- array * returns: number of files found */
- #define ALLOC_SIZE 10
- int Directory( char *filespec, unsigned attribute, struct find_t
- **filesPtr )
- {
- int number_found = 0,
- allocated = 0;
- struct find_t tmpBuff;
-
- if( _dos_findfirst( filespec, attribute, &tmpBuff ) ) {
- /* it wasn't suitable*/
- *filesPtr = NULL; /* set it NULL just for safety */
- return 0;
- }
- do {
- if( number_found >= allocated)
- (*filesPtr) = resizeArray( *filesPtr, (allocated +=
- ALLOC_SIZE));
- (*filesPtr)[number_found++] = tmpBuff; }
- while( !_dos_findnext(&tmpBuff) );
- resizeArray( *filesPtr, number_found); /* get rid of any extras */
- return number_found;
- }
- /* SortFiles() generally sorts files with qsort, which uses one of
- * five different compare functions depending on user
- * input. That's been cut out, leaving the part
- * that will reverse a sort. I want to reverse the
- * sort by reversing what the pointers point to, but
- * seem unable to make this work. */
-
- void SortFiles( struct dir_window *dwindow, int (*compare)(const
- void *elem1, const void *elem2))
- {
- int i, h;
- struct find_t temp, **files = dwindow->files;
-
- /* There's a qsort here I've removed as extraneous */
- if( reverse_sort ) { /* make it all backwards */
- #if defined SHOULD_WORK /* told this should work, but doesn't */
- struct find_t *temp;
- for (i=0; i < dwindow->numberfiles / 2; i++) {
- temp = files[i];
- files[i] = files[dwindow->numberfiles - i - 1];
- files[dwindow->numberfiles - i - 1] = temp;
- }
- #else
- for( i = 0; i < dwindow->numberfiles / 2; i++ ) {
- /* now reverse the entire array */
- temp = (*files)[i];
- (*files)[i] = (*files)[dwindow->numberfiles - i - 1];
- (*files)[dwindow->numberfiles - i - 1] = temp;
- }
- #endif
- }
- }
- /* GetDirectory() loads a list of directories into files and returns
- the number of listings*/
- void
- PrintDir( struct dir_window window )
- {
- int i;
- struct find_t **files = window.files;
-
- for( i = 0; i < window.numberfiles; i++ )
- puts( (*files)[i].name );
- }
- int GetDirectory( struct dir_window *dwindow, struct find_t **files )
- {
- dwindow->numberfiles = Directory( "*.*", 0xffff, files );
- if( dwindow->numberfiles < 1 )
- return 0;
- dwindow->files = files;
- SortFiles( dwindow, sorts[sort_proc] );
- PrintDir( window );
- return dwindow->numberfiles;
- }
- main( int argc, char *argv[] )
- {
- if( !(window.numberfiles = GetDirectory( &window, &files) ) ) {
- fprintf( stderr, "no files found" );
- return 1;
- }
- }
- /***************** CUT HERE **********************/
-